
public class CircularLinkedList<E>
{
   private Node<E> head = null;
   private Node<E> last;
   private Node<E> first; 
   
   class Node<E>
   {
      private Node<E> next;
      private E data;
      
      
      public Node(E data)
      {
         this.data = data;
      }
      public Node(E data, Node<E> next)
      {
         this.data = data;
         this.next = next;
      }
   }
   
   public CircularLinkedList()
   {
      
   }
   public void add(E data)
   {
      if(head == null)
      {
         head = new Node<E>(data,head);
         last = head;
         
      }
      else
         head = new Node<E>(data,head);
         last.next = first = head;
      
   }
   public String toString()
   {
      boolean theend = false;
      String result = "head";
      Node<E> temp = head;
      
      /*
      while(!theend)
      {
         result += "==>" + head.data;
         head = head.next;
         if(head.data.equals(first.data))
         {
            theend = true;
         }
      }*/
      
      for(int i=0;i<5;i++)
      {
         result += "==>" + temp.data;
         temp = temp.next;
      }
      
      return result;
   }
   public String head()
   {
      return (String)head.data;
   }
   public String last()
   {
      return (String)last.data;
   }
   public String lastnext()
   {
      return (String)last.next.data;
   }
   public void delete()
   {     
      Node<E> third = head.next.next;
      Node<E> second = head.next;
      System.out.println("third: "+third.data);
      System.out.println("second: "+second.data);

      if(last.data.equals(third.data))
      {
         last = second;
      }
      second.next = third.next;

   }
   
}
